<?php
$back_link = $_SERVER['HTTP_REFERER']; 
$karyawan_id = check_karyawan_by_nik($_GET['nik_karyawan']);
if(!$karyawan_id){
	my_direct($back_link);
}

if($_GET['pr'] == 'prev'){
	$not = '<';
	$order = ' DESC ';
}else{
	$not = '>';
	
	$order = ' ASC ';
}
$query = "
			SELECT a.karyawan_id FROM karyawan a 
			INNER JOIN biodata b ON a.biodata_id = b.biodata_id
			WHERE a.nik {$not} '{$_GET['nik_karyawan']}' 
			AND karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian)  
			ORDER BY a.nik {$order} LIMIT 1 ";
$result = my_query($query);
if(!(my_num_rows($result) > 0)){
	
	if($_GET['pr'] == 'prev'){ 
		$query = "
			SELECT MAX(karyawan_id) as karyawan_id FROM karyawan a 
			INNER JOIN biodata b ON a.biodata_id = b.biodata_id
			WHERE karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian)  
			ORDER BY a.nik LIMIT 1 ";
	}else{ 
		$query = "
			SELECT MIN(karyawan_id) as karyawan_id FROM karyawan a 
			INNER JOIN biodata b ON a.biodata_id = b.biodata_id
			WHERE  karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian)  
			ORDER BY a.nik LIMIT 1 ";
	}
}
$result = my_query($query);
$row = my_fetch_array($result);
$new_karyawan_id = $row['karyawan_id'];		
	
$parse = end(explode("?" , $back_link));
$params = explode("&",$parse);
 
$newlink = '';
foreach($params as $param){   
	list($key,$val) = explode("=",$param);
	if($key =='karyawan_id'){
		$newlink.='&'.$key.'='.$new_karyawan_id ;
	}elseif($key =='id'){
		$newlink.='&'.$key.'='.$new_karyawan_id ;
	}else{
		if(trim($key) <>'')
		$newlink.='&'.$key.'='.$val;
	}
} 
my_direct('index.php?'.$newlink);
exit;